//
//  LZPMainVC.m
//  网易新闻
//
//  Created by Batac_Lee on 16/4/20.
//  Copyright © 2016年 SAN. All rights reserved.
//

#import "LZPMainVC.h"
#import "LZPVC1.h"
#import "LZPVC2.h"
#import "LZPVC3.h"
#import "LZPVC4.h"
#import "LZPVC5.h"
#import "LZPVC6.h"

#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
@interface LZPMainVC ()<UIScrollViewDelegate>
@property(weak, nonatomic)UIButton * currentBtn;
@property(weak, nonatomic)UIScrollView * topScrollView;
@property(weak, nonatomic)UIScrollView * mainScrollView;
@property(strong, nonatomic)NSMutableArray * btnArray;
@end
@implementation LZPMainVC
//该数组用于保存topScrollView中的button;
-(NSMutableArray *)btnArray{
    if (!_btnArray) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}
-(void)viewDidLoad{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self addAllChildVC];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setMainView];
    [self setTopScrollView];
    self.title = @"网易新闻";
    [self touchBtn:self.currentBtn];
}
//设置主ScrollView,
-(void)setMainView{
    CGFloat x = 0;
    CGFloat y = 64 + 40;
    CGFloat W = ScreenW;
    CGFloat H = ScreenH - y;
    UIScrollView * mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(x, y, W, H)];
    mainScrollView.contentSize = CGSizeMake(self.childViewControllers.count * ScreenW, H);
    mainScrollView.showsHorizontalScrollIndicator = NO;
    mainScrollView.pagingEnabled = YES;
    mainScrollView.bounces = NO;
    mainScrollView.delegate = self;
    [self.view addSubview:mainScrollView];
    _mainScrollView = mainScrollView;
}
//设置新闻titlescrollView
-(void)setTopScrollView{
    CGFloat x = 0;
    CGFloat y = self.navigationController.navigationBarHidden? 20 : 64;
    CGFloat W = ScreenW;
    CGFloat H = 40;
    
    UIScrollView * topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(x, y, W, H)];
    topScrollView.contentSize = CGSizeMake(ScreenW * 6.0 / 4, 40);
    topScrollView.showsHorizontalScrollIndicator = NO;
    topScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:topScrollView];
    _topScrollView = topScrollView;
    [self setBtn:topScrollView];
    [self setLine:topScrollView];//下方的分割线
}
//设置topscrollView下边的一条分割线
-(void)setLine:(UIScrollView *)topScrollView{
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(-100, 39, topScrollView.contentSize.width + 200, 1)];
    lineView.backgroundColor = [UIColor blackColor];
    lineView.alpha = 0.3;
    [topScrollView addSubview:lineView];
}
//设置top上的button,用于显示新闻title
-(void)setBtn:(UIScrollView *)topScrollView{
    NSInteger count = self.childViewControllers.count;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat W = ScreenW / 4.0;
    CGFloat H = topScrollView.frame.size.height;
    for (int i = 0; i < count; i ++) {
        x = i * W;
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIViewController * vc = self.childViewControllers[i];
        btn.tag = i;
        btn.frame = CGRectMake(x, y, W, H);
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [topScrollView addSubview:btn];
        [self.btnArray addObject:btn];
        [btn addTarget:self action:@selector(touchBtn:) forControlEvents:UIControlEventTouchDown];
        [self setChildVC:i];
        if (0 == btn.tag) {
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            btn.transform = CGAffineTransformMakeScale(1.3, 1.3);
        }else{
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
}
//在主scrollView上面添加子view;每个模块的view都会并排着添加到主控制器的view上;
-(void)setChildVC:(NSInteger)i{
    UIViewController * vc = self.childViewControllers[i];
//    if (vc.view.subviews)return;//!!!!!!!!!!!!!!!
    vc.view.frame = CGRectMake(i * ScreenW,0, ScreenW, ScreenH - (64 + 40));
    [_mainScrollView addSubview:vc.view];
}
//按钮(top上的新闻title)的监听方法
-(void)touchBtn:(UIButton *)btn{
    [self.currentBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    CGFloat x =  ( btn.tag + 0.5 )* btn.frame.size.width - 0.5 * ScreenW;
    
    if (x < 0) {
        x = 0;
    }
    CGFloat maxX = _topScrollView.contentSize.width - ScreenW;
    if ( x > maxX) {
        x = maxX;
    }

    [UIView animateWithDuration:0.3 animations:^{
        _topScrollView.contentOffset = CGPointMake(x, 0);
    }];
    _mainScrollView.contentOffset = CGPointMake(btn.tag * ScreenW, 0);
 
    self.currentBtn = btn;
  
}
//delegate
//使用scrollView的代理方法,实现主scrollView滚动,top中的title字体大小变化和颜色变化;
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger leftPage = scrollView.contentOffset.x / ScreenW;
    NSInteger rightPage = leftPage + 1;
    UIButton * leftBtn = self.btnArray[leftPage];
    UIButton * rightBtn;
    if (rightPage < self.btnArray.count) {
        rightBtn = self.btnArray[rightPage];
    }
    
    CGFloat scalR = scrollView.contentOffset.x / ScreenW - leftPage;
    CGFloat scalL = 1 - scalR;
    
    leftBtn.transform = CGAffineTransformMakeScale(scalL * 0.3 + 1, scalL * 0.3 + 1);
    rightBtn.transform = CGAffineTransformMakeScale(scalR * 0.3 + 1, scalR * 0.3 + 1);
    [leftBtn setTitleColor:[UIColor colorWithRed:scalL green:0 blue:0 alpha:1] forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor colorWithRed:scalR green:0 blue:0 alpha:1] forState:UIControlStateNormal];
}
//设置主scrollView的滚动带动topscrollView的滚动;
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //实现vc滚动带着title跟着滚动;
    NSInteger currentPageNumber =  scrollView.contentOffset.x / ScreenW;
    UIButton * btn = self.btnArray[currentPageNumber];
    [self touchBtn:btn];
    [self setChildVC:currentPageNumber];
}
//添加子控制器
//最好一个view对应一个控制器,不然可能有些时间没法处理;
-(void)addAllChildVC{
    LZPVC1 * vc1 = [[LZPVC1 alloc] init];
    vc1.view.backgroundColor = [UIColor greenColor];
    vc1.title = @"头条";
    [self addChildViewController:vc1];
    
    LZPVC2 * vc2 = [[LZPVC2 alloc] init];
    vc2.view.backgroundColor = [UIColor magentaColor];
    vc2.title = @"热点";
    [self addChildViewController:vc2];
    
    LZPVC3 * vc3 = [[LZPVC3 alloc] init];
    vc3.view.backgroundColor = [UIColor orangeColor];
    vc3.title = @"视频";
    [self addChildViewController:vc3];
    
    LZPVC4 * vc4 = [[LZPVC4 alloc] init];
    vc4.view.backgroundColor = [UIColor redColor];
    vc4.title = @"社会";
    [self addChildViewController:vc4];
    
    LZPVC5 * vc5 = [[LZPVC5 alloc] init];
    vc5.view.backgroundColor = [UIColor purpleColor];
    vc5.title = @"订阅";
    [self addChildViewController:vc5];
    
    LZPVC6 * vc6 = [[LZPVC6 alloc] init];
    vc6.view.backgroundColor = [UIColor blueColor];
    vc6.title = @"科技";
    [self addChildViewController:vc6];

}
@end
